package com.lizard.schedule.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lizard.schedule.R;

/**
 * Schedule自定义风格Alert对话框，支持一个、两个按钮
 */
public class ScheduleAlertDialog extends Dialog {

    private Builder mBuilder;

    private ScheduleAlertDialog(Context context) {
        super(context, R.style.ScheduleDialogTheme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.schedule_alert_dialog);

        TextView messageTv = (TextView)findViewById(R.id.message);
        messageTv.setText(TextUtils.isEmpty(mBuilder.mMessage) ? "" : mBuilder.mMessage);

        Button positiveBtn = (Button)findViewById(R.id.positive_btn);
        Button negativeBtn = (Button)findViewById(R.id.negative_btn);

        if(!TextUtils.isEmpty(mBuilder.mPositiveButtonName)){
            positiveBtn.setText(mBuilder.mPositiveButtonName);
            positiveBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    cancel();
                    if(mBuilder.mPositiveButtonClick != null){
                        mBuilder.mPositiveButtonClick.onClick(view);
                    }
                }

            });
            positiveBtn.setVisibility(View.VISIBLE);
        } else{
            positiveBtn.setVisibility(View.GONE);
        }

        if(!TextUtils.isEmpty(mBuilder.mNegativeButtonName)){
            negativeBtn.setText(mBuilder.mNegativeButtonName);
            negativeBtn.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View view) {
                    cancel();
                    if(mBuilder.mNegativeButtonClick != null){
                        mBuilder.mNegativeButtonClick.onClick(view);
                    }
                }
            });
            negativeBtn.setVisibility(View.VISIBLE);
        } else{
            negativeBtn.setVisibility(View.GONE);
        }

        if(positiveBtn.getVisibility() == View.VISIBLE && negativeBtn.getVisibility() == View.VISIBLE){
            positiveBtn.setBackgroundResource(R.drawable.schedule_alert_dialog_left_button_bg);
            negativeBtn.setBackgroundResource(R.drawable.schedule_alert_dialog_right_button_bg);
        } else{
            if(positiveBtn.getVisibility() == View.VISIBLE){
                positiveBtn.setBackgroundResource(R.drawable.schedule_alert_dialog_button_bg);
            } else if(negativeBtn.getVisibility() == View.VISIBLE){
                negativeBtn.setBackgroundResource(R.drawable.schedule_alert_dialog_button_bg);
            }
        }
    }

    public static class Builder{

        /**
         * 第一个按钮名
         */
        private String mPositiveButtonName;

        /**
         * 第一个按钮点击事件
         */
        private View.OnClickListener mPositiveButtonClick;

        /**
         * 第二个按钮名
         */
        private String mNegativeButtonName;

        /**
         * 第二个按钮点击事件
         */
        private View.OnClickListener mNegativeButtonClick;

        /**
         * 对话框内容文字
         */
        private String mMessage;

        private Context mContext;

        public Builder(Context context){
            mContext = context;
        }

        /**
         * 第二个按钮文本和点击事件设置
         *
         * @param buttonText
         * @param onClickListener
         * @return
         */
        public Builder setNegativeButton(String buttonText, View.OnClickListener onClickListener){
            mNegativeButtonName = buttonText;
            mNegativeButtonClick = onClickListener;
            return this;
        }

        /**
         * 第二个按钮文本和点击事件设置
         *
         * @param buttonTextId
         * @param onClickListener
         * @return
         */
        public Builder setNegativeButton(int buttonTextId, View.OnClickListener onClickListener){
            return setNegativeButton(mContext.getResources().getString(buttonTextId), onClickListener);
        }

        /**
         * 第一个按钮文本和点击事件设置
         *
         * @param buttonText
         * @param onClickListener
         * @return
         */
        public Builder setPositiveButton(String buttonText, View.OnClickListener onClickListener){
            mPositiveButtonName = buttonText;
            mPositiveButtonClick = onClickListener;
            return this;
        }

        /**
         * 第一个按钮文本和点击事件设置
         *
         * @param buttonTextId
         * @param onClickListener
         * @return
         */
        public Builder setPositiveButton(int buttonTextId, View.OnClickListener onClickListener){
            return setPositiveButton(mContext.getResources().getString(buttonTextId), onClickListener);
        }

        /**
         * 设置对话框内容文字
         *
         * @param message
         * @return
         */
        public Builder setMessage(String message){
            mMessage = message;
            return this;
        }

        /**
         * 设置对话框内容文字
         * @param messageId
         * @return
         */
        public Builder setMessage(int messageId){
           return setMessage(mContext.getResources().getString(messageId));
        }

        /**
         * 显示对话框
         */
        public void show(){
            ScheduleAlertDialog scheduleAlertDialog = new ScheduleAlertDialog(mContext);
            scheduleAlertDialog.mBuilder = this;
            scheduleAlertDialog.show();
        }

    }
}
